#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include "data_type.h"
#include "cube.h"
#include "cube_define.h"
#include "cube_record.h"

#include "user_define.h"
#include "node_define.h"
#include "data_define.h"

// add para lib_include
#include "transfer_node.h"


int proc_dispatch(void * sub_proc,void * message);

int transfer_node_init(void * sub_proc,void * para)
{
	int ret;
	// add youself's plugin init func here
	return 0;
}

int transfer_node_start(void * sub_proc,void * para)
{
	int ret;
	void * recv_msg;
	int i;
	int type;
	int subtype;


	while(1)
	{
		usleep(time_val.tv_usec);
		ret=ex_module_recvmsg(sub_proc,&recv_msg);
		if(ret<0)
			continue;
		if(recv_msg==NULL)
			continue;
		type=message_get_type(recv_msg);
		subtype=message_get_subtype(recv_msg);
		if(!memdb_find_recordtype(type,subtype))
		{
			printf("message format (%d %d) is not registered!\n",
				message_get_type(recv_msg),message_get_subtype(recv_msg));
			continue;
		}
		if((type==TYPE(NODE_DEFINE))&&(subtype==SUBTYPE(NODE_DEFINE,USER)))
		{
			ret=proc_nodeuser_addtarget(sub_proc,recv_msg);
		}
		if((type==TYPE(EVENT_DEFINE))&&(subtype==SUBTYPE(EVENT_DEFINE,EVENT_CONTROL)))
		{
			ret=proc_eventcontrol_addtarget(sub_proc,recv_msg);
		}
	}
	return 0;
}

int proc_nodeuser_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(NODE_DEFINE,BASIC_INFO) * node_info;
	RECORD(NODE_DEFINE,USER) * node_user;
	
	DB_RECORD * db_record;

    ret = message_get_record(recv_msg,&node_user,0);
    if(ret<0)
        return ret;
    
	target=Talloc0(sizeof(*target));

	db_record=memdb_find_first(TYPE_PAIR(NODE_DEFINE,BASIC_INFO),"node_name",node_user->node_name);
    if(db_record==NULL)
    {
           print_cubeerr("transfer_node: node %s has not connected yet!\n",node_user->node_name);
           return -EINVAL;
    }

	node_info=db_record->record;

	comp_proc_uuid(node_info->node_uuid,node_info->node_name,target->uuid);

    ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
    if(ret<0)
         return ret;
    ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}

int proc_eventcontrol_addtarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(NODE_DEFINE,BASIC_INFO) * node_info;
    RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	
	DB_RECORD * db_record;


    ret=message_get_record(recv_msg,&event_control,0);
    if(ret<0)
           return ret;

	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(NODE_DEFINE,BASIC_INFO),"node_name",event_control->node_name);
    if(db_record==NULL)
    {
           print_cubeerr("transfer_node: node %s has not connected yet!\n",event_control->node_name);
           return -EINVAL;
    }

	node_info=db_record->record;

	comp_proc_uuid(node_info->node_uuid,node_info->node_name,target->uuid);

    ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
    if(ret<0)
         return ret;
    ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}
/*
int proc_expand_addnodetarget(void * sub_proc,void * recv_msg)
{
	int ret;

	RECORD(MESSAGE,UUID_RECORD) * target;
	RECORD(NODE_DEFINE,CONFIG) * node_config;
    RECORD(EVENT_DEFINE,EVENT_CONTROL) * event_control;
	RECORD(NODE_DEFINE,BASIC_INFO) * node_info;
	
	DB_RECORD * db_record;

    MSG_EXPAND * msg_expand;

    ret=message_get_define_expand(recv_msg,&msg_expand,TYPE_PAIR(NODE_DEFINE,CONFIG));
    if(ret<0)
           return ret;

    node_config=msg_expand->expand;       
	target=Talloc0(sizeof(*target));
	db_record=memdb_find_first(TYPE_PAIR(NODE_DEFINE,BASIC_INFO),"node_name",node_config->node_name);
    if(db_record==NULL)
    {
           print_cubeerr("transfer_node: node %s has not connected yet!\n",node_config->node_name);
           return -EINVAL;
    }

	node_info=db_record->record;

	comp_proc_uuid(node_info->node_uuid,node_info->node_name,target->uuid);

    ret=message_add_expand_data(recv_msg,TYPE_PAIR(MESSAGE,UUID_RECORD),target);
    if(ret<0)
         return ret;
    ex_module_sendmsg(sub_proc,recv_msg);
	return 0;
}*/
